<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include 'secure/access_securities.php';
class Control_employer_contact_persons extends Access_securities {

	public function __construct(){
		parent::__construct("control-employers");
		$this -> form_validation -> set_error_delimiters('<p class="help-block danger">', '</p>');
	}
	public function index()
	{
		$data['title']="Manage the employer contact persons";
		$data['employer_contact_persons']=$this->model_globals->select_all('contact_persons')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function insert_employer_contact_person(){
		$data['title']="Create Employer contact person";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[employers.email]');
		$this->form_validation->set_rules('name', 'Name', 'required|min_length[2]|max_length[50]');
		//$this->form_validation->set_rules('phone', 'Phone', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['contact_persons']=$this->model_globals->select_all('contact_persons')->result();
			$this->load->view(B_MASTER."index",$data);
		} else {
			$data_employer_contact_person=array(
				'using_employer_id'=>$this->input->post('employer',TRUE),
				'email'=>$this->input->post('email',TRUE),
				'name'=>$this->input->post('name',TRUE),
				'phone'=>$this->input->post('phone',TRUE),
				'status'=>1,
				'created_date'=>null
			);
			
			
			$is_inserted_employer_contact_person=$this->model_globals->insert('contact_persons',$data_employer_contact_person);
			if($is_inserted_employer_contact_person){
				
				$this -> session -> set_flashdata('success', 'New employer  contact person was created success !.');
				redirect('control-employer-contact-persons');
				
			}else{
				$this -> session -> set_flashdata('error', 'New employer contact person  was created fail !.');
				redirect('control-employer-contact-person');
			}
		}
	}
	public function update_employer_contact_person(){
		$data['title']="Update Employer";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		
		$employer_contact_person_id=$this->uri->segment(3);
		
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[employers.email]');
		$this->form_validation->set_rules('name', 'Name', 'required|min_length[2]|max_length[50]');
		//$this->form_validation->set_rules('phone', 'Phone', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['employer_contact_person']=$this->model_globals->select_where('contact_persons',array('contact_person_id'=>$employer_contact_person_id))->result();
			$data['employers']=$this->model_globals->select_all('employers')->result();
			$this->load->view(B_MASTER."index",$data);
		} else {
			$data_employer_contact_person=array(
				'using_employer_id'=>$this->input->post('employer',TRUE),
				'email'=>$this->input->post('email',TRUE),
				'name'=>$this->input->post('name',TRUE),
				'phone'=>$this->input->post('phone',TRUE)
			);
			
			
			$is_updated_employer_contact_person=$this->model_globals->update('contact_persons',$data_employer_contact_person,array('contact_person_id'=>$employer_contact_person_id));
			if($is_updated_employer_contact_person){
				
				$this -> session -> set_flashdata('success', 'New employer  contact person was updated success !.');
				redirect('control-employer-contact-persons');
				
			}else{
				$this -> session -> set_flashdata('error', 'New employer contact person  was updated fail !.');
				redirect('control-employer-contact-persons');
			}
		}
	}
	public function delete_employer_contact_person(){
		$employer_contact_person_id=$this->uri->segment(3);
		$is_deleted=$this->model_globals->delete('contact_persons',array('contact_person_id'=>$employer_contact_person_id));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'Employer contact person information was deleted success !.');
			redirect('control-employer-contact-persons');
		}else{
			$this -> session -> set_flashdata('error', 'Employer contact person information  was deleted fail !.');
			redirect('control-employer-contact-persons');
		}
	}
	public function disabled_employer(){
		$employer_contact_person_id=$this->uri->segment(3);
		$data=array('status'=>0);
		$is_disabled=$this->model_globals->update('contact_person',$data,array('contact_person_id'=>$employer_contact_person_id));
		if($is_disabled){
			$this -> session -> set_flashdata('success', 'Employeer  contact person was disabled success !.');
			redirect('control-employer-contact-person');
		}else{
			$this -> session -> set_flashdata('error', 'Employeer contact person  was disabled fail !.');
			redirect('control-employer-contact-person');
		}
	}
	public function enabled_employer(){
		$employer_contact_person_id=$this->uri->segment(3);
		$data=array('status'=>1);
		$is_disabled=$this->model_globals->update('contact_person',$data,array('contact_person_id'=>$employer_contact_person_id));
		if($is_disabled){
			$this -> session -> set_flashdata('success', 'Employeer  contact person was enabled success !.');
			redirect('control-employer-contact-person');
		}else{
			$this -> session -> set_flashdata('error', 'Employeer contact person  was enabled fail !.');
			redirect('control-employer-contact-person');
		}
	}
	
}
